<template>
  <div class="app-main-container">
    <Breadcrumb name="限时折扣" />
    <div class="warnningTip">
      <i class="el-icon-warning-outline" />
      限时折扣功能，目前支持针对套餐、课程、专项卡进行配置，配置完成后，需要在<span
        class="primaryColor pointer underline"
        @click="handleMini"
      >小程序装修</span>页面配置呈现形式和内容。
    </div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clear"
    />
    <!-- <div v-if="meta.indexOf('CouponAdd') > -1" class="doumee-table-buttons"> -->
    <div class="doumee-table-buttons">
      <el-button type="primary" @click="handleEdit()">新增</el-button>
      <el-button @click="handleSubscription()">查看订阅记录</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        label="活动名称"
        prop="name"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="商品类型" min-width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.businessType == '04'">套餐</span>
          <span v-if="scope.row.businessType == '07'">课程</span>
          <span v-if="scope.row.businessType == '19'">专项卡</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="活动时间" width="180">
        <template slot-scope="scope">
          <div>起：{{ scope.row.startTime }}</div>
          <div>止：{{ scope.row.endTime }}</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="活动状态"
        prop=""
        show-overflow-tooltip
        min-width="80"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status == '0'">未开始</span>
          <span v-if="scope.row.status == '1'">进行中</span>
          <span v-if="scope.row.status == '2'">已结束</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="活动价格"
        prop="discountPrice"
        show-overflow-tooltip
        min-width="80"
      />
      <el-table-column
        align="center"
        label="活动订单数"
        prop=""
        show-overflow-tooltip
        min-width="100"
      >
        <template v-slot="scope">
          <span class="primaryColor pointer" @click="orderClick(scope.row)">{{
            scope.row.sale_count
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="剩余库存"
        prop="surplus"
        show-overflow-tooltip
        min-width="80"
      />
      <el-table-column
        align="center"
        label="备注"
        prop="remark"
        show-overflow-tooltip
        min-width="120"
      />
      <el-table-column
        label="操作"
        min-width="150"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- v-if="meta.indexOf('CouponDel') > -1" -->
          <div class="flex-cc">
            <el-button
              v-if="scope.row.status == '0' || scope.row.status == '1'"
              v-preventReClick
              type="text"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              v-if="scope.row.status == '0' || scope.row.status == '1'"
              v-preventReClick
              type="text"
              @click="handlePreview(scope.row)"
            >海报</el-button>
            <el-button
              v-if="scope.row.status == '1'"
              v-preventReClick
              type="text"
              style="color: red"
              @click="toEnd(scope.row.id)"
            >结束</el-button>
            <el-button
              v-if="scope.row.status == '0' || scope.row.status == '2'"
              v-preventReClick
              type="text"
              style="color: red"
              @click="toDelete(scope.row.id)"
            >删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="totalCount"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <Edit
      v-if="isShowEdit"
      ref="EditRef"
      @success="getList"
      @close="isShowEdit = false"
    />
    <Detail ref="DetailRef" />
    <el-dialog title="海报下载" :visible.sync="isShowAvatar" width="600px">
      <div>
        下载海报/二维码，可推广至微信好友、朋友圈等，或打印活动海报，张贴至门店，吸引顾客参与活动。
      </div>
      <div style="text-align: center">
        <el-image
          style="width: 300px"
          :src="activeRow.poster"
          :preview-src-list="[activeRow.poster]"
        />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleDown('0')">下载二维码</el-button>
        <el-button type="primary" @click="handleDown('0')">下载海报</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  SecKillList,
  venueList,
  SecKillDel,
  SecKillEnd,
  SecKillAvatar
} from '@/api'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
import Edit from './edit.vue'
import Detail from './detail.vue'
import Breadcrumb from '../components/Breadcrumb.vue'
export default {
  components: {
    Pagination,
    Edit,
    Detail,
    Breadcrumb
  },
  data() {
    return {
      isShowEdit: false,
      isShowAvatar: false,
      visible: false,
      queryFormConfig,
      totalCount: 0,
      list: [],
      activeRow: {},
      meta: [],
      listLoading: true,
      venueList: [],
      orderType: [],
      pagination: {
        page: 1,
        rows: 10
      },
      suitableVenue: [],
      filters: {}
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
    // this.getOrderTypeList()
    this.getList(1)
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    handleEdit(row) {
      console.log('row', row)
      this.isShowEdit = true
      this.$nextTick(() => {
        if (row && row.id) {
          this.$refs.EditRef.getDetail(row)
        }
        this.$refs.EditRef.isShowModal = true
      })
    },
    handleSubscription() {
      this.$router.push({
        name: 'SecDiscountMemberList'
      })
    },
    orderClick(row) {
      this.$router.push({
        name: 'SecDiscountOrderList',
        query: {
          id: row.id
        }
      })
    },
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: {}
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          // this.venueList = res.recordList
          const arr = res.recordList
          arr.forEach(e => {
            e.label = e.name
            e.value = e.id
          })
          this.queryFormConfig.formItems[3].options = arr
        })
        .catch(() => { })
    },

    getList(page) {
      this.listLoading = true
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      SecKillList({
        pagination,
        param: { ...filters, type: '1' }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.totalCount = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    // 删除
    toDelete(id) {
      const that = this
      this.$confirm('此操作将删除该秒杀活动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then((res) => {
          SecKillDel({ param: { id }}).then((res) => {
            if (res.errorCode !== '000000') return
            that.$message.success('删除成功')
            that.getList()
          })
        })
        .catch(() => {
          // this.$message.info('已取消删除')
        })
    },
    handlePreview(row) {
      this.activeRow = {}
      this.isShowAvatar = true
      SecKillAvatar({ param: { id: row.id }}).then(res => {
        this.activeRow = res.record || ''
      })
    },
    handleDown(str) {
      if (str === '0') {
        window.open(this.activeRow.poster, '_blank')
      } else {
        window.open(this.activeRow.ewm, '_blank')
      }
      this.isShowAvatar = false
    },
    toEnd(id) {
      const that = this
      this.$confirm('此操作将结束该秒杀活动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then((res) => {
          SecKillEnd({ param: { id }}).then((res) => {
            if (res.errorCode !== '000000') return
            that.$message.success('结束成功')
            that.getList()
          })
        })
        .catch(() => {
          // this.$message.info('已取消删除')
        })
    },
    // 清空
    clear() {
      this.filters = {}
      this.getList(1)
    },
    handleMini() {
      this.$router.push('/application/miniFinish')
    }
  }
}
</script>
